package com.tjsinfo.tjsoa.system.bo;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.tjsinfo.tjsoa.Dao.IHibernateDao;
import com.tjsinfo.tjsoa.Dao.IQbcDao;
import com.tjsinfo.tjsoa.system.dao.ITSystemRolesGroupDAO;
import com.tjsinfo.tjsoa.system.vo.TSystemRolesGroup;
import com.tjsinfo.tjsoa.system.vo.TSystemUserRoleGroup;

public class SystemUserRoleGroupBO implements ITSystemUserRoleGroupBO {
	private IQbcDao qbcDao;
	private ITSystemRolesGroupBO systemRolesGroupBO;
	private IHibernateDao hibernateDao;
	
	public IHibernateDao getHibernateDao() {
		return hibernateDao;
	}

	public void setHibernateDao(IHibernateDao hibernateDao) {
		this.hibernateDao = hibernateDao;
	}

	public ITSystemRolesGroupBO getSystemRolesGroupBO() {
		return systemRolesGroupBO;
	}

	public void setSystemRolesGroupBO(ITSystemRolesGroupBO systemRolesGroupBO) {
		this.systemRolesGroupBO = systemRolesGroupBO;
	}

	public IQbcDao getQbcDao() {
		return qbcDao;
	}

	public void setQbcDao(IQbcDao qbcDao) {
		this.qbcDao = qbcDao;
	}

	public List<TSystemRolesGroup> selectByUserId(Integer userid) {
		// TODO Auto-generated method stub
		List<TSystemRolesGroup> rolesgrouplist = new ArrayList<TSystemRolesGroup>();
		DetachedCriteria criteria = DetachedCriteria.forClass(TSystemUserRoleGroup.class);
		criteria.add(Restrictions.eq("userId", userid));
		List<TSystemUserRoleGroup> list = qbcDao.getCriteria(criteria);
		for (TSystemUserRoleGroup systemUserRoleGroup : list) {
			TSystemRolesGroup systemRolesGroup = systemRolesGroupBO.selectById(systemUserRoleGroup.getRoleGroupId());
			rolesgrouplist.add(systemRolesGroup);
		}
		return rolesgrouplist;
	}

	public Integer addUserRoleGroup(TSystemUserRoleGroup systemUserRoleGroup) {
		// TODO Auto-generated method stub
		return hibernateDao.save(systemUserRoleGroup);
	}
	
	public List<TSystemUserRoleGroup> selectUserRoleGroupByUserId(Integer userid){
		List<TSystemUserRoleGroup> rolesgrouplist = new ArrayList<TSystemUserRoleGroup>();
		DetachedCriteria criteria = DetachedCriteria.forClass(TSystemUserRoleGroup.class);
		criteria.add(Restrictions.eq("userId", userid));
		List<TSystemUserRoleGroup> list = qbcDao.getCriteria(criteria);
		for (TSystemUserRoleGroup systemUserRoleGroup : list) {
			TSystemRolesGroup systemRolesGroup = systemRolesGroupBO.selectById(systemUserRoleGroup.getRoleGroupId());
			systemUserRoleGroup.setRoleName(systemRolesGroup.getGroupname());
			rolesgrouplist.add(systemUserRoleGroup);
		}
		return rolesgrouplist;
	}

	public Boolean deleteUserRoleGroupByUserId(Integer userid) {
		// TODO Auto-generated method stub
		try {
			List<TSystemUserRoleGroup> list = selectUserRoleGroupByUserId(userid);
			for (TSystemUserRoleGroup systemUserRoleGroup : list) {
				hibernateDao.delete(systemUserRoleGroup);
				
			}
			return true;
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		
	}

}
